<?php

namespace App\Http\Resources;

use App\Models\Pool;
use Carbon\Carbon;

class OrderListResource extends Resource
{
    /**
     * Transform the resource collection into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        $pool = Pool::where('name', $this->pool_name)->first();
        return [
            'id' => $this->id,
            'pool_name' => $this->pool_name,
            'number' => $this->number,
            'profit' => bcadd($pool->profit, 0, 2),
            'predict_profit' => bcmul($this->number * $this->month * 30, $pool->profit, 2),
            'remain_days' => ($this->status != 1) ? 0 : max((new Carbon)->diffInDays(Carbon::parse($this->finish_at), false), 0), // 托管周期剩余天数
            'allow_renew' => ($this->status == 1) ? 1 : 0, // 是否可以续费
            'allow_cancel' => ($this->status == 1 && now()->subDays(5) <= $this->created_at) ? 1 : 0, // 是否可以取消
            'price' => bcadd($pool->price, 0, 2), // 续费价格USDT
            'status' => ($this->status == -1) ? 2 : $this->status,
        ];
    }
}
